package com.fast.fastenjoy.base.manager;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;

import com.fast.fastenjoy.base.dao.BaseDao;
import com.fast.fastenjoy.base.dao.EntityAware;
import com.fast.fastenjoy.base.utils.PackageUtils;

/**
 * 处理父类
 * @author chenliaohua
 *
 * @param <T>
 */
public abstract class EntityManager<T extends Serializable> implements EntityAware<T>{
	
	private Class<T> entityClass;
	
	private String entityName;
	
	@Autowired
	protected BaseDao<T> baseDao;
	
	@PostConstruct
	@SuppressWarnings("unchecked")
	public void setEntityClass(){
		Type t = PackageUtils.getGenericEntityClass(this.getClass());
		if(t != null && t instanceof Class){
			this.entityClass = (Class<T>)t;
			entityName = this.entityClass.getSimpleName();
		}
	}
	
	public Serializable save(T t){
		 return baseDao.save(t);
	}
	
	public List<T> getAll(){
		
		return baseDao.getAll(entityClass);
	}
	
	public String getEntityName(){
		
		return entityName;
	}
	
	public Class<T> getEntityClass() {
		
		return this.entityClass;
	}
	
	public BaseDao<T> getBaseDao() {  
        return baseDao;  
    }

}
